<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>分配角色</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form">
		<div class="layui-form-item" id="boxRoleForm">
		
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="addWelfares">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</div>
	</form>
	<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- 	<script type="text/javascript" src="/js/user/newsAdd.js"></script> -->
	
	<script type="text/javascript">
	layui.config({
		 base: '/js/admin/'
	}).extend({
	    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
	    $tool: 'tool'
	}).use(['form','layer','jquery','ajaxExtention','$tool'],function(){
		var form = layui.form,
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			$ = layui.jquery;
		 $tool = layui.$tool;

		//创建一个编辑器
	 	form.on("submit(addWelfares)",function(data){
	 		var datas=data.field;
	 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
	 		$.ajax({  
	            type: "POST",  
	            url:"/welfares",  
	            data:JSON.stringify(datas),// 序列化表单值  
	         //   dataType: "json",
	            contentType:"application/json",
	           
	            success: function(res) {  
	            	if(res.code==2000){
	            		top.layer.close(index);
	          			top.layer.msg("添加成功！");
	           			layer.closeAll("iframe");
	          	 		//刷新父页面
	          	 		parent.location.reload();
	            	}
	            
	            } ,
		 		 error: function(request) {  
		             alert("Connection error");  
		         } 
	            
	            
	        }); 
	 		return false;
	 	})
	 	initUserInfo();
	 	 function initUserInfo() {
	        var queryArgs = $tool.getQueryParam();//获取查询参数
	        var id = queryArgs['id'];
	        allotRole(id);
    	}
	 	
	 	  //弹出选择角色的框
	    function allotRole(id){
	        $.ajax({
	            async:false,
	            type : "POST",
	            data:{uid:id},
	            url: '/roles/rolesWithSelected',
	            dataType:'json',
	            success: function(data){
	                $("#boxRoleForm").empty();
	                var htm = "<input type='hidden' name='userid' value='"+id+"'>";
	                for(var i=0;i<data.length;i++){
	                    htm += "<div class='checkbox'><label><input type='checkbox' name='roleid' value='"+data[i].id+"'";
	                    if(data[i].selected==1){
	                        htm += " checked='checked'";
	                    }
	                    htm +="/>"+data[i].roledesc+"</label></div>";
	                }
	                $("#boxRoleForm").append(htm);
	                form.render();//重新绘制表单，让修改生效
	            }
	        });
	 	  }
	})
	</script>
</body>
</html>

